package com.autonomous_printer.controller;

import com.autonomous_printer.pojo.File;
import com.autonomous_printer.service.FileService;
import com.autonomous_printer.service.PrintService;
import com.autonomous_printer.util.FileTypeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.Random;

@Controller
public class FileController {

    @Autowired
    PrintService printService;

    @Autowired
    FileService fileService;

    @Autowired
    FileTypeUtil fileTypeUtil;

    private Random random = new Random();

    @GetMapping("/uploadPage")
    public String gotoUploadPage() {
        return "uploadPage";
    }


    @PostMapping("/upload")
    public String uploadQuestion(@RequestParam("uploadFile") MultipartFile uploadFile, Model model) {

        //如果不是pdf或者word文件直接返回
        if (!fileTypeUtil.isPDF(uploadFile.getOriginalFilename()) && !fileTypeUtil.isWORD(uploadFile.getOriginalFilename())) {
            return "index";
        }
        File file = new File(uploadFile.getOriginalFilename(), "./file/" + random.nextInt(10000) + uploadFile.getOriginalFilename(), new Date(System.currentTimeMillis()));
        java.io.File parentFile = new java.io.File("./file");
        InputStream inputStream;
        int fileId = -1;
        try {
            inputStream = uploadFile.getInputStream();
            if (!parentFile.exists())
                parentFile.mkdirs();
            try {
                fileId = fileService.upload(file, inputStream);
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                inputStream.close();
            }
        } catch (IOException e) {
            return "index";
        }
        String filepath = fileService.convertToPDF(file.getFileLie());
        int page = fileService.getPdfPage("./file/" + filepath);
        model.addAttribute("fileId", fileId);
        model.addAttribute("filepath", filepath);
        model.addAttribute("page", page);
        return "print";
    }

    @PostMapping("/print")
    public String print(@RequestParam("filepath") String filepath,
                        @RequestParam("page") int page,
                        @RequestParam("fileId") int fileId,
                        @RequestParam("printPageType") String printPageType,
                        @RequestParam("printColorType") String printColorType,
                        @RequestParam("printCount") int printCount,
                        Model model) {
        //如果答应参数不符合规定,返回打印页面
        boolean flag = false;
        if (!"double".equals(printPageType) && !"single".equals(printPageType)) {
            flag=true;
        }
        if(!"color".equals(printColorType) && !"none".equals(printColorType)){
            flag=true;
        }
        if(printCount<1){
            flag=true;
        }
        if (flag) {
            model.addAttribute("fileId", fileId);
            model.addAttribute("filepath", filepath);
            model.addAttribute("page", page);
            return "print";
        }
        String id = printService.prePrint(page, fileId, printPageType, printColorType, printCount);

        return "redirect:pay?id=" + id;
    }

}
